import { getToken } from "@/utils/token";
import axios, {
    AxiosError,
    InternalAxiosRequestConfig,
    AxiosResponse,
} from "axios";
export const BASE_URL = "http://82.156.8.154:4533/";
// export const BASE_URL = "http://192.168.31.12:4533/";

// 创建axios实例
const axiosInstance = axios.create({
    baseURL: BASE_URL,
    timeout: 5000,
});

// 添加请求拦截器
axiosInstance.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // content-type
        config.headers["Content-Type"] = "application/json;charset=utf-8";
        // 授权token
        config.headers["x-nd-authorization"] = getToken();

        return config;
    },
    (error: AxiosError) => {
        return Promise.reject(error);
    }
);

// 响应拦截
axiosInstance.interceptors.response.use(
    (response: AxiosResponse) => {
        return response;
    },
    (error: AxiosError) => {
        const err = {
            msg: error.response?.data,
            status: error.status,
        };
        return Promise.reject(err);
    }
);

export default axiosInstance;
